package com.yixin.ystartlibrary.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Locale;
import java.util.TimeZone;

/**
 * User: Ystar
 * Date: 2018/1/28
 * Content: 时间便捷
 */
public class CalendarUtils {

    private static final String TIME_ZONE_CHINA = "Asia/Shanghai";

    public static final String FORMAT_YMDHMS = "yyyy-MM-dd HH:mm:ss.";
    public static final String FORMAT_YMDHM = "yyyy/MM/dd HH:mm";
    public static final String FORMAT_YMD = "yyyy/MM/dd";
    public static final String FORMAT_HMS = "HH:mm:ss";
    public static final String FORMAT_MD = "MM-dd";
    public static final String FORMAT_HM = "HH:mm";
    public static final String FORMAT_YM = "yyyy-MM";

    /**
     * 获取时间的format显示,format:时:分:秒.
     *
     * @param millis 毫秒值
     * @return
     */
    public static String getGregorianCalendarTime(long millis) {
        return getGregorianCalendarFormat(millis, FORMAT_YMDHMS);
    }

    /**
     * 获取时间的format显示, format:年-月-日.
     *
     * @param millis 毫秒值
     * @return
     */
    public static String getGregorianCalendarDate(long millis) {
        return getGregorianCalendarFormat(millis, FORMAT_YMDHMS);
    }

    /**
     * 获取时间的format显示,format:yyyy-MM-dd HH:mm:ss.
     *
     * @param millis 毫秒值
     * @return
     */
    public static String getGregorianCalendarFormat(long millis, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINA);
        GregorianCalendar gc = getGregorianCalendar();
        gc.setTimeInMillis(millis);
        return sdf.format(gc.getTime());
    }

    /**
     * 获取时间的format显示,format:yyyy-MM-dd HH:mm:ss.
     *
     * @param date Date
     * @return
     */
    public static String getDateGregorianCalendarFormat(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINA);
        GregorianCalendar gc = getGregorianCalendar();
        gc.setTimeInMillis(date.getTime());
        return sdf.format(gc.getTime());
    }

    /**
     * 获取默认时间的format显示,format:yyyy-MM-dd HH:mm:ss.
     *
     * @return
     */
    public static String getDefaultGregorianCalendarFormat(String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format, Locale.CHINA);
        GregorianCalendar gc = getGregorianCalendar();
        return sdf.format(gc.getTime());
    }

    /**
     * 获取中国时区的time控件.
     *
     * @return GregorianCalendar
     */
    public static GregorianCalendar getGregorianCalendar() {
        return new GregorianCalendar(TimeZone.getTimeZone(TIME_ZONE_CHINA));
    }

    /**
     * 日期时间 "yyyy/MM/dd HH:mm" 转为时间戳
     * @param timeString 日期时间
     * @return 时间戳
     */
    public static long getTime(String timeString){
        SimpleDateFormat sdf = new SimpleDateFormat(FORMAT_YMDHM, Locale.CHINA);
        Date d;
        long l = 0;
        try{
            d = sdf.parse(timeString);
            l = d.getTime();
        } catch(ParseException e){
            e.printStackTrace();
        }
        return l;
    }
    public  static  long getTime2(String timestring) throws ParseException {
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = simpleDateFormat.parse(timestring);
        long ts = date.getTime();
       return ts ;

    }

}
